// app
#include "robot_def.h"
#include "robot_cmd.h"
// module
#include "remote_control.h"
#include "ins_task.h"
#include "master_process.h"
#include "message_center.h"
#include "general_def.h"
#include "dji_motor.h"
#include "can_comm.h"
#include "referee_task.h"
// bsp
#include "bsp_dwt.h"
#include "bsp_log.h"

/*刷新ui   按键检测   摩擦滑轮开启  */



// 私有宏,自动将编码器转换成角度值
#define YAW_ALIGN_ANGLE -137.1801458f  // 对齐时的角度,0-360 
//底盘板的角度是-137.1801458f
//云台版的角度是118.827278f
#define PTICH_HORIZON_ANGLE 3.29587507 // pitch水平时电机的角度,0-36
//3.29587507

#ifdef SENTRY_GIMBAL
#define YAW_MAX_ANGLE 100.0f
#define YAW_MIN_ANGLE -100.0f       
#endif // DEBUG

/* cmd应用包含的模块实例指针和交互信息存储*/
#ifdef GIMBAL_BOARD // 对双板的兼容,条件编译
#include "can_comm.h"
static CANCommInstance *cmd_can_comm; // 双板通信
#endif
#ifdef ONE_BOARD
static Publisher_t *chassis_cmd_pub;   // 底盘控制消息发布者
static Subscriber_t *chassis_feed_sub; // 底盘反馈信息订阅者
#endif                                 // ONE_BOARD

#ifdef SENTRY_CHASISS
static Publisher_t *chassis_cmd_pub;   // 底盘控制消息发布者
static Subscriber_t *chassis_feed_sub; // 底盘反馈信息订阅者
#endif   

static Chassis_Ctrl_Cmd_s chassis_cmd_send;      // 发送给底盘应用的信息,包括控制信息和UI绘制相关
static Chassis_Upload_Data_s chassis_fetch_data; // 从底盘应用接收的反馈信息信息,底盘功率枪口热量与底盘运动状态等

static RC_ctrl_t *rc_data;              // 遥控器数据,初始化时返回
static Vision_Recv_s *vision_recv_data; // 视觉接收数据指针,初始化时返回
static Vision_Send_s vision_send_data;  // 视觉发送数据
static RV_Receive_Packet_s *vision_recv_data_zhong;//中南的接受数据
static RV_Send_Packet_s vision_send_data_zhong;//中南的通信发送

static Publisher_t *gimbal_cmd_pub;            // 云台控制消息发布者
static Subscriber_t *gimbal_feed_sub;          // 云台反馈信息订阅者
static Gimbal_Ctrl_Cmd_s gimbal_cmd_send;   // 传递给云台的控制信息
static Gimbal_Upload_Data_s gimbal_fetch_data; // 从云台获取的反馈信息

static Publisher_t *shoot_cmd_pub;           // 发射控制消息发布者
static Subscriber_t *shoot_feed_sub;         // 发射反馈信息订阅者
static Shoot_Ctrl_Cmd_s shoot_cmd_send;      // 传递给发射的控制信息
static Shoot_Upload_Data_s shoot_fetch_data; // 从发射获取的反馈信息

static Robot_Status_e robot_state; // 机器人整体工作状态

//双板任务初始化
//底盘板兼主控板的通信任务
#ifdef SENTRY_CHASISS
static CANCommInstance *chasiss_can_comm;//底盘板通信实例
static CANCommData_s *chasiss_can_recv;//底盘板接收的数据，更改传输数据在robot_def里面
static CANCommData_s chasiss_can_send;//底盘板发送数据，注意发送和接收的指针符号
#endif // SENTRY_CHASISS
//云台版的通信任务
#ifdef SENTRY_GIMBAL
static CANCommInstance *gimbal_can_comm;//云台板通信实例
static CANCommData_s *gimbal_can_recv;//云台板接收的数据，更改传输数据在robot_def里面
static CANCommData_s gimbal_can_send;//云台板发送数据，注意发送和接收的指针符号
static float yaw_aim_aps = 0.15f;
static float pitch_aim_aps = 0.15f;
#endif // DEBUG

//其他逻辑初始化
static uint16_t dwt;//计时，超过一定时间就启动寻找目标模式

void RobotCMDInit()
{
//底盘板的初始化封装    
#ifdef SENTRY_CHASISS
    rc_data = RemoteControlInit(&huart3);//遥控器初始化，目标只有一个遥控操作
    // vision_recv_data = VisionInit(&huart1); // 视觉通信串口，湖大的通信初始化，需要时开启
    vision_recv_data_zhong = RV_VisionInit(&huart1);// 视觉通信串口，中南的通信初始化，需要时开启
    //底盘的板内通信，需要大Yaw的数据反馈
    gimbal_cmd_pub = PubRegister("gimbal_cmd", sizeof(Gimbal_Ctrl_Cmd_s));
    gimbal_feed_sub = SubRegister("gimbal_feed", sizeof(Gimbal_Upload_Data_s));
    chassis_cmd_pub = PubRegister("chassis_cmd", sizeof(Chassis_Ctrl_Cmd_s));
    chassis_feed_sub = SubRegister("chassis_feed", sizeof(Chassis_Upload_Data_s));
    //双板通信初始化
    CANComm_Init_Config_s comm_conf = {
        .can_config = {
            .can_handle = &hcan2,
            .tx_id = 0x312,
            .rx_id = 0x311,
        },
        .recv_data_len = sizeof(CANCommData_s),
        .send_data_len = sizeof(CANCommData_s),
    };
    chasiss_can_comm = CANCommInit(&comm_conf);

    robot_state = ROBOT_READY; // 启动时机器人进入工作模式,后续加入所有应用初始化完成之后再进入
#endif // SENTRY_CHASISS

#ifdef SENTRY_GIMBAL
//云台板不接遥控器，所以不进行遥控的初始化
    gimbal_cmd_pub = PubRegister("gimbal_cmd", sizeof(Gimbal_Ctrl_Cmd_s));
    gimbal_feed_sub = SubRegister("gimbal_feed", sizeof(Gimbal_Upload_Data_s));
    shoot_cmd_pub = PubRegister("shoot_cmd", sizeof(Shoot_Ctrl_Cmd_s));
    shoot_feed_sub = SubRegister("shoot_feed", sizeof(Shoot_Upload_Data_s));
//双板通信初始化
    CANComm_Init_Config_s comm_conf = {
        .can_config = {
            .can_handle = &hcan2,
            .tx_id = 0x311,
            .rx_id = 0x312,
        },
        .recv_data_len = sizeof(CANCommData_s),
        .send_data_len = sizeof(CANCommData_s),
    };
    gimbal_can_comm = CANCommInit(&comm_conf);
    robot_state = ROBOT_STOP;//使用双板进行启动

#endif // DEBUG

    // rc_data = RemoteControlInit(&huart3);   // 修改为对应串口,注意如果是自研板dbus协议串口需选用添加了反相器的那个
    // vision_recv_data = VisionInit(&huart1); // 视觉通信串口

    // gimbal_cmd_pub = PubRegister("gimbal_cmd", sizeof(Gimbal_Ctrl_Cmd_s));
    // gimbal_feed_sub = SubRegister("gimbal_feed", sizeof(Gimbal_Upload_Data_s));
    // shoot_cmd_pub = PubRegister("shoot_cmd", sizeof(Shoot_Ctrl_Cmd_s));
    // shoot_feed_sub = SubRegister("shoot_feed", sizeof(Shoot_Upload_Data_s));

#ifdef ONE_BOARD // 双板兼容
    chassis_cmd_pub = PubRegister("chassis_cmd", sizeof(Chassis_Ctrl_Cmd_s));
    chassis_feed_sub = SubRegister("chassis_feed", sizeof(Chassis_Upload_Data_s));
#endif // ONE_BOARD
#ifdef GIMBAL_BOARD
    CANComm_Init_Config_s comm_conf = {
        .can_config = {
            .can_handle = &hcan1,
            .tx_id = 0x312,
            .rx_id = 0x311,
        },
        .recv_data_len = sizeof(Chassis_Upload_Data_s),
        .send_data_len = sizeof(Chassis_Ctrl_Cmd_s),
    };
    cmd_can_comm = CANCommInit(&comm_conf);
#endif // GIMBAL_BOARD
    // gimbal_cmd_send.pitch = 5;


    // robot_state = ROBOT_READY; // 启动时机器人进入工作模式,后续加入所有应用初始化完成之后再进入
}



/**
 * @brief 根据gimbal app传回的当前电机角度计算和零位的误差
 *        单圈绝对角度的范围是0~360,说明文档中有图示
 *
 */
static void CalcOffsetAngle()
{
    // 别名angle提高可读性,不然太长了不好看,虽然基本不会动这个函数
    static float angle;

    //static float Angle;
    angle = gimbal_fetch_data.yaw_motor_single_round_angle; // 从云台获取的当前yaw电机单圈角度
    
   //Angle  = gimbal_fetch_data.gimbal_imu_data.Pitch;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#if YAW_ECD_GREATER_THAN_4096                               // 如果大于180度
    if (angle > YAW_ALIGN_ANGLE && angle <= 180.0f + YAW_ALIGN_ANGLE)
        chassis_cmd_send.offset_angle = angle - YAW_ALIGN_ANGLE;
    else if (angle > 180.0f + YAW_ALIGN_ANGLE)
        chassis_cmd_send.offset_angle = angle - YAW_ALIGN_ANGLE - 360.0f;
    else
        chassis_cmd_send.offset_angle = angle - YAW_ALIGN_ANGLE;
#else // 小于180度
    if (angle > YAW_ALIGN_ANGLE)
        chassis_cmd_send.offset_angle = angle - YAW_ALIGN_ANGLE;
    else if (angle <= YAW_ALIGN_ANGLE && angle >= YAW_ALIGN_ANGLE - 180.0f)
        chassis_cmd_send.offset_angle = angle - YAW_ALIGN_ANGLE;
    else
        chassis_cmd_send.offset_angle = angle - YAW_ALIGN_ANGLE + 360.0f;
        //chassis_cmd_send.offset_angle = angle + (-angle - 180);

#endif
}

/**
 * @brief 控制输入为遥控器(调试时)的模式和控制量设置
 *
 */
static void RemoteControlSet()
{
    // 控制底盘和云台运行模式,云台待添加,云台是否始终使用IMU数据?
    if (switch_is_down(rc_data[TEMP].rc.switch_right)) // 右侧开关状态[下],底盘跟随云台
    {
        chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW;
        gimbal_cmd_send.gimbal_mode = GIMBAL_GYRO_MODE;
        shoot_cmd_send.friction_mode = FRICTION_ON;
    }
    else if (switch_is_mid(rc_data[TEMP].rc.switch_right)) // 右侧开关状态[中],底盘和云台分离,底盘保持不转动
    {
        chassis_cmd_send.chassis_mode = CHASSIS_NO_FOLLOW;
        gimbal_cmd_send.gimbal_mode = GIMBAL_FREE_MODE;
        shoot_cmd_send.friction_mode = FRICTION_ON;
        //gimbal_feed_sub.aaa = PITCH_MAX_ANGLE ;
    }  
    else if (switch_is_up(rc_data[TEMP].rc.switch_right)) // 右侧开关状态[上],小陀螺
    {
        chassis_cmd_send.chassis_mode = CHASSIS_ROTATE;
        gimbal_cmd_send.gimbal_mode = GIMBAL_FREE_MODE;
        shoot_cmd_send.friction_mode = FRICTION_ON;
    }   
    
    // 云台参数,确定云台控制数据
    // if (switch_is_mid(rc_data[TEMP].rc.switch_left)) // 左侧开关状态为[中],视觉模式
    // {
    //     // 待添加,视觉会发来和目标的误差,同样将其转化为total angle的增量进行控制
    //     // ...
    // }
    // 左侧开关状态为[下],或视觉未识别到目标,纯遥控器拨杆控制
    if (switch_is_down(rc_data[TEMP].rc.switch_left) || vision_recv_data->target_state == NO_TARGET)
    { // 按照摇杆的输出大小进行角度增量,增益系数需调整
        // gimbal_cmd_send.yaw += -0.005f * (float)rc_data[TEMP].rc.rocker_l_; //0.005 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#ifdef SENTRY_CHASISS
/**
 * @brief 哨兵的遥控器操作逻辑，当地盘模式为小陀螺和跟随模式时，云台板启动云台瞄准模式
 *        在云台分离模式中，大yaw固定不动，此时可以控制小yaw，小yaw的启动为将左侧拨到中间就可以了
 *
 */
        if(gimbal_cmd_send.gimbal_mode == GIMBAL_FREE_MODE && chassis_cmd_send.chassis_mode == CHASSIS_NO_FOLLOW)
        {
        // chasiss_can_send.yaw = vision_recv_data_zhong->yaw;
        // chasiss_can_send.pitch = vision_recv_data_zhong->pitch;
        // chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
        // chasiss_can_send.shoot_mode = SHOOT_ON;
        // chasiss_can_send.friction_mode = FRICTION_ON;
        chasiss_can_send.robot_status = ROBOT_READY;
        // gimbal_cmd_send.yaw = (vision_recv_data->yaw == 0 ? gimbal_cmd_send.yaw : vision_recv_data->yaw);
        // gimbal_cmd_send.pitch = (vision_recv_data->pitch == 0 ? gimbal_cmd_send.pitch : vision_recv_data->pitch);
        chasiss_can_send.yaw = -0.001f * (float)rc_data[TEMP].rc.rocker_l_;
        chasiss_can_send.pitch = -0.001f * (float)rc_data[TEMP].rc.rocker_l1;
        chasiss_can_send.small_gimbal_mode =  GIMBAL_FREE_MODE;
        gimbal_cmd_send.yaw = 0.0f; 
        }
        else if(gimbal_cmd_send.gimbal_mode == GIMBAL_FREE_MODE && chassis_cmd_send.chassis_mode == CHASSIS_ROTATE)
        {
        chasiss_can_send.small_gimbal_mode = GIMBAL_AIM_MODE;
        gimbal_cmd_send.yaw += -0.001f * (float)rc_data[TEMP].rc.rocker_l_;
        }
        else if(gimbal_cmd_send.gimbal_mode == GIMBAL_GYRO_MODE)
        {
        chasiss_can_send.small_gimbal_mode = GIMBAL_AIM_MODE;
        gimbal_cmd_send.yaw += -0.001f * (float)rc_data[TEMP].rc.rocker_l_;
        }

#endif // SENTRY_CHASISS        

    }
    // 云台软件限位
    static float Angle;
    Angle  = gimbal_fetch_data.gimbal_imu_data.Pitch;
    if(Angle > 20.9 )
    {
        Angle = 20.9;
        gimbal_cmd_send.pitch = Angle - 3;
    }
    if(Angle < -20.9 )
    {
        Angle =-20.9;
        gimbal_cmd_send.pitch = Angle + 2;
    }
    // 底盘参数,目前没有加入小陀螺(调试似乎暂时没有必要),系数需要调整
    chassis_cmd_send.vx = 70.0f * (float)rc_data[TEMP].rc.rocker_r_; // _水平方向
    chassis_cmd_send.vy = 70.0f * (float)rc_data[TEMP].rc.rocker_r1; // 1数值方向



    // 发射参数
    // 弹舱舵机控制,待添加servo_motor模块,开启
    // 弹舱舵机控制,待添加servo_motor模块,关闭
    // 摩擦轮控制,拨轮向上打为负,向下为正
    // 拨弹控制,遥控器固定为一种拨弹模式,可自行选择
#ifdef SENTRY_CHASISS
    if (rc_data[TEMP].rc.dial < -400) //
        chasiss_can_send.load_mode = LOAD_BURSTFIRE;
    else
        chasiss_can_send.load_mode = LOAD_STOP;
    // 射频控制,固定每秒1发,后续可以根据左侧拨轮的值大小切换射频,
    shoot_cmd_send.shoot_rate = 8;
    // if(chassis_fetch_data.rest_heat_one <= 20)
    //     {
    //         shoot_cmd_send.load_mode = LOAD_STOP;
    //         shoot_cmd_send.shoot_mode = SHOOT_OFF;
    //         shoot_cmd_send.friction_mode = FRICTION_OFF;
    //     }
    //     else if(chassis_fetch_data.rest_heat_one >= 100)
    //     {
    //         shoot_cmd_send.shoot_mode = SHOOT_ON;
    //         shoot_cmd_send.friction_mode = FRICTION_ON;
    //     }
#endif // SENTRY_CHASISS 
}

/**
 * @brief 输入为键鼠时模式和控制量设置
 *
 */
static void MouseKeySet()
{
    chassis_cmd_send.vx = (rc_data[TEMP].key[KEY_PRESS].a * 300 - rc_data[TEMP].key[KEY_PRESS].d * 300)*70; // 系数待测
    chassis_cmd_send.vy = (rc_data[TEMP].key[KEY_PRESS].w * 300 - rc_data[TEMP].key[KEY_PRESS].s * 300)*70;

    gimbal_cmd_send.yaw -= (float)rc_data[TEMP].mouse.x / 660 * 30; // 系数待测
    gimbal_cmd_send.pitch += (float)rc_data[TEMP].mouse.y / 660 * 10;

    //shoot_cmd_send.friction_mode = FRICTION_ON; //摩擦轮常开

/*UI刷新*/
    switch (rc_data[TEMP].key[KEY_PRESS].v)
    {
    case 0:
        chassis_cmd_send.ui_mode = UI_KEEP;
        break;
    default:
        chassis_cmd_send.ui_mode = UI_REFRESH;
        break;
    }
//    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_Z] % 3) // Z键设置弹速
//    {
//    case 0:
//        shoot_cmd_send.bullet_speed = 15;
//        break;
//    case 1:
//        shoot_cmd_send.bullet_speed = 18;
//        break;
//    default:
//        shoot_cmd_send.bullet_speed = 30;
//        break;
//    }
    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_Z] % 3) // Z键设置模式
    {
    case 0: //底盘和云台分离 摩擦轮开启
        chassis_cmd_send.chassis_mode = CHASSIS_NO_FOLLOW;
        gimbal_cmd_send.gimbal_mode = GIMBAL_FREE_MODE;
        shoot_cmd_send.friction_mode = FRICTION_ON;
        break;
    case 1:
        chassis_cmd_send.chassis_mode = CHASSIS_FOLLOW_GIMBAL_YAW;
        gimbal_cmd_send.gimbal_mode = GIMBAL_FREE_MODE;
        shoot_cmd_send.friction_mode = FRICTION_ON;
        break;

    default: //小陀螺
        chassis_cmd_send.chassis_mode = CHASSIS_ROTATE;
        gimbal_cmd_send.gimbal_mode = GIMBAL_FREE_MODE;
        shoot_cmd_send.friction_mode = FRICTION_ON;
       
        break;
    }
/*
switch (rc_data[TEMP].key_count[KEY_PRESS][Key_E] % 4) // E键设置发射模式
    {
    case 0:
        shoot_cmd_send.load_mode = LOAD_STOP;
        break;
    case 1:
        shoot_cmd_send.load_mode = LOAD_1_BULLET;
        break;
    case 2:
        shoot_cmd_send.load_mode = LOAD_3_BULLET;
        break;
    default:
        shoot_cmd_send.load_mode = LOAD_BURSTFIRE;
        break;
    }
*/    

/*单击鼠标左键发射*/
if(shoot_cmd_send.friction_mode = FRICTION_ON)
{
     
    if(rc_data[TEMP].mouse.press_l)
    {
        shoot_cmd_send.load_mode = LOAD_BURSTFIRE;
    }
    if(!(rc_data[TEMP].mouse.press_l))
    {
        shoot_cmd_send.load_mode = LOAD_STOP;
    }
}
   

//    if(rc_data[TEMP].key_count[KEY_PRESS][Key_Q])
//    {
//       chassis_cmd_send.chassis_mode = CHASSIS_ROTATE; 
//    }
//    if(!(rc_data[TEMP].key_count[KEY_PRESS][Key_Q]))
//    {
//        chassis_cmd_send.chassis_mode = CHASSIS_NO_FOLLOW;
//    }

/*
 switch (rc_data[TEMP].key_count[KEY_PRESS][Key_R] % 2) // R键开关弹舱
    {
    case 0:
        shoot_cmd_send.lid_mode = LID_OPEN;
        break;
    default:
        shoot_cmd_send.lid_mode = LID_CLOSE;
        break;
    }
*/   

    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_R] % 2) // R键重启机器人
    {
    case 0:
        robot_state = ROBOT_READY;
        break;
    default:
        robot_state = ROBOT_STOP;
        break;
    }

    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_F] % 2) // F键开关摩擦轮
    {
    case 0:
        shoot_cmd_send.friction_mode = FRICTION_OFF;
        break;
    default:
        shoot_cmd_send.friction_mode = FRICTION_ON;
        break;
    }

    switch (rc_data[TEMP].key_count[KEY_PRESS][Key_C] % 4) // C键设置底盘速度
    {
    case 0:
        chassis_cmd_send.chassis_speed_buff = 40;
        break;
    case 1:
        chassis_cmd_send.chassis_speed_buff = 60;
        break;
    case 2:
        chassis_cmd_send.chassis_speed_buff = 80;
        break;
    default:
        chassis_cmd_send.chassis_speed_buff = 100;
        break;
    }
    switch (rc_data[TEMP].key[KEY_PRESS].shift) // 待添加 按shift允许超功率 消耗缓冲能量
    {
    case 1:

        break;

    default:

        break;
    }
}

/**
 * @brief 机载电脑控制模式和小yaw启动模式，在未连接记载电脑时作为小yaw的启动，
 *        发送一个固定的数据给小yaw，当连接机载电脑时开始控制小yaw
 * 
 * @todo  暂时记载电脑通信未建立，考虑将中南的视觉通信加入到module层中
 */
#ifdef SENTRY_CHASISS
static void Computer_Control_Set()
{
    //板内的数据发送
    //首先是雷达到底盘板的速度映射
    chassis_cmd_send.chassis_mode = vision_recv_data_zhong->chassis_mode;       //底盘模式选择CHASSIS_NO_FOLLOW
    chassis_cmd_send.vx = vision_recv_data_zhong->vel_y * -1000 / RADIUS_WHEEL / PI2 * 360.0f * REDUCTION_RATIO_WHEEL;                
    //Vx的速度映射,vx对应雷达的vy
    chassis_cmd_send.vy = vision_recv_data_zhong->vel_x * 1000 / RADIUS_WHEEL / PI2 * 360.0f * REDUCTION_RATIO_WHEEL;
    if(chassis_fetch_data.current_HP < 400)
    {
        chassis_cmd_send.chassis_mode = CHASSIS_ROTATE;
    }
    if(chassis_fetch_data.game_progress == 5)
    {
        chassis_cmd_send.chassis_mode = CHASSIS_NO_FOLLOW;
    }
    //Vy的速度映射，Vy对应雷达的vx
    // chassis_cmd_send.vx = vision_recv_data_zhong->vel_y * -1;
    // chassis_cmd_send.vy = vision_recv_data_zhong->vel_x * 1;
    gimbal_cmd_send.yaw = 0;//云台板固定不动
    gimbal_cmd_send.gimbal_mode = GIMBAL_FREE_MODE;
    //小云台的映射逻辑
    if(vision_recv_data_zhong->gimbal_select == 0)
    {
        chasiss_can_send.yaw = vision_recv_data_zhong->yaw;
        chasiss_can_send.pitch = vision_recv_data_zhong->pitch - 1;
        
        if(vision_recv_data_zhong->fire_mode == AUTO_AIM && chassis_fetch_data.game_progress == 4 && chassis_fetch_data.rest_heat_one >= 50)
        {
            dwt = 0;
            chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
            chasiss_can_send.load_mode = LOAD_BURSTFIRE;
            chasiss_can_send.shoot_mode = SHOOT_ON;
            chasiss_can_send.friction_mode = FRICTION_ON;
        }
        else
        {
            chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
            chasiss_can_send.load_mode = LOAD_STOP;
            chasiss_can_send.shoot_mode = SHOOT_ON;
            chasiss_can_send.friction_mode = FRICTION_ON;
        }
        // else if(vision_recv_data_zhong->fire_mode == AUTO_PRED && chassis_fetch_data.game_progress == 4 && chassis_fetch_data.rest_heat_one >= 50)
        if(vision_recv_data_zhong->fire_mode == AUTO_PRED && chassis_fetch_data.game_progress == 4 && chassis_fetch_data.rest_heat_one >= 50)
        {
            dwt = 0;
            chasiss_can_send.yaw = vision_recv_data_zhong->yaw;
            chasiss_can_send.pitch = vision_recv_data_zhong->pitch - 3;
            chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
            chasiss_can_send.load_mode = LOAD_BURSTFIRE;
            chasiss_can_send.shoot_mode = SHOOT_ON;
            chasiss_can_send.friction_mode = FRICTION_ON;
        }
        else
        {
            chasiss_can_send.yaw = vision_recv_data_zhong->yaw;
            chasiss_can_send.pitch = vision_recv_data_zhong->pitch - 3;
            chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
            chasiss_can_send.load_mode = LOAD_STOP;
            chasiss_can_send.shoot_mode = SHOOT_ON;
            chasiss_can_send.friction_mode = FRICTION_ON;
        }
        if(vision_recv_data_zhong->fire_mode == AUTO_FIRE)
        {
            dwt = 0;
            chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
            chasiss_can_send.load_mode = LOAD_STOP;
            chasiss_can_send.shoot_mode = SHOOT_ON;
            chasiss_can_send.friction_mode = FRICTION_ON;
        }
        if(vision_recv_data_zhong->fire_mode == NO_FIRE)
        {
            dwt++;
            chasiss_can_send.yaw =  chasiss_can_recv->yaw;
            chasiss_can_send.pitch =  chasiss_can_recv->pitch;
            chasiss_can_send.small_gimbal_mode = GIMBAL_GYRO_MODE;
            chasiss_can_send.load_mode = LOAD_STOP;
            chasiss_can_send.shoot_mode = SHOOT_ON;
            chasiss_can_send.friction_mode = FRICTION_ON;
            if(dwt > 100)
            {
                    chasiss_can_send.small_gimbal_mode = GIMBAL_AIM_MODE;
            }
        }
        if(switch_is_up(rc_data[TEMP].rc.switch_right))
        {
            if (rc_data[TEMP].rc.dial < -400) //
            chasiss_can_send.load_mode = LOAD_BURSTFIRE;
            else
            chasiss_can_send.load_mode = LOAD_STOP;
            // 射频控制,固定每秒1发,后续可以根据左侧拨轮的值大小切换射频,
            shoot_cmd_send.shoot_rate = 8;
        }
    }
    
    chasiss_can_send.robot_status = ROBOT_READY;
}
#endif // SENTRY_CHASISS


/**
 * @brief 将主控发送的数据进行板内映射
 * 
 * @todo  将云台版的工作状态进行赋值
 */
#ifdef SENTRY_GIMBAL
static void SET_ROBOT()
{
    gimbal_cmd_send.gimbal_mode = gimbal_can_recv->small_gimbal_mode;
    robot_state = gimbal_can_recv->robot_status;
    // if(gimbal_can_recv->yaw > 0)
    // {
    //     yaw_aim_aps = 0.15f;
    // }
    // if(gimbal_can_recv->yaw < 0)
    // {
    //     yaw_aim_aps = -0.15f;
    // }
    if (gimbal_cmd_send.gimbal_mode == GIMBAL_GYRO_MODE)
    {
        gimbal_cmd_send.yaw = gimbal_can_recv->yaw;
        gimbal_cmd_send.pitch = gimbal_can_recv->pitch;
        if (gimbal_cmd_send.pitch > PITCH_MAX_ANGLE)
            gimbal_cmd_send.pitch = PITCH_MAX_ANGLE;
        if (gimbal_cmd_send.pitch < PITCH_MIN_ANGLE)
            gimbal_cmd_send.pitch = PITCH_MIN_ANGLE;
        if (gimbal_cmd_send.yaw > YAW_MAX_ANGLE)
            gimbal_cmd_send.yaw = YAW_MAX_ANGLE;
        if (gimbal_cmd_send.yaw < YAW_MIN_ANGLE)
            gimbal_cmd_send.yaw = YAW_MIN_ANGLE;
    }
    else if (gimbal_cmd_send.gimbal_mode == GIMBAL_FREE_MODE)
    {
        gimbal_cmd_send.yaw += gimbal_can_recv->yaw;
        gimbal_cmd_send.pitch += gimbal_can_recv->pitch;
        if (gimbal_cmd_send.pitch > PITCH_MAX_ANGLE)
            gimbal_cmd_send.pitch = PITCH_MAX_ANGLE;
        if (gimbal_cmd_send.pitch < PITCH_MIN_ANGLE)
            gimbal_cmd_send.pitch = PITCH_MIN_ANGLE;
        if (gimbal_cmd_send.yaw > YAW_MAX_ANGLE)
            gimbal_cmd_send.yaw = YAW_MAX_ANGLE;
        if (gimbal_cmd_send.yaw < YAW_MIN_ANGLE)
            gimbal_cmd_send.yaw = YAW_MIN_ANGLE;
    }
    else if (gimbal_cmd_send.gimbal_mode == GIMBAL_AIM_MODE)
    {
        if (gimbal_cmd_send.yaw > YAW_MAX_ANGLE)
            yaw_aim_aps = -0.2f;
        if (gimbal_cmd_send.yaw < YAW_MIN_ANGLE)
            yaw_aim_aps = 0.2f;
        if (gimbal_cmd_send.pitch > 5)
            pitch_aim_aps = -0.1f;
        if (gimbal_cmd_send.pitch < -10)
            pitch_aim_aps = 0.1f;
        gimbal_cmd_send.yaw += yaw_aim_aps;
        gimbal_cmd_send.pitch += pitch_aim_aps;
    }
    shoot_cmd_send.friction_mode = gimbal_can_recv->friction_mode;
    shoot_cmd_send.shoot_mode = gimbal_can_recv->shoot_mode;
    shoot_cmd_send.load_mode = gimbal_can_recv->load_mode;
    shoot_cmd_send.shoot_rate = 6.0f;
    // shoot_cmd_send.shoot_mode = SHOOT_OFF;
    // shoot_cmd_send.friction_mode = FRICTION_OFF;
    // shoot_cmd_send.load_mode = LOAD_STOP;
    gimbal_can_send.yaw = gimbal_fetch_data.gimbal_imu_data.YawTotalAngle;
    gimbal_can_send.pitch = gimbal_fetch_data.gimbal_imu_data.Pitch; 
}
#endif // SENTRY_GIMBAL


/**
 * @brief  紧急停止,包括遥控器左上侧拨轮打满/重要模块离线/双板通信失效等
 *         停止的阈值'300'待修改成合适的值,或改为开关控制.
 *
 * @todo   后续修改为遥控器离线则电机停止(关闭遥控器急停),通过给遥控器模块添加daemon实现
 *
 */
static void EmergencyHandler()
{
#ifdef SENTRY_CHASISS
    // 拨轮的向下拨超过一半进入急停模式.注意向打时下拨轮是正
    if (rc_data[TEMP].rc.dial > 300 || robot_state == ROBOT_STOP) // 还需添加重要应用和模块离线的判断
    {
        robot_state = ROBOT_STOP;
        gimbal_cmd_send.gimbal_mode = GIMBAL_ZERO_FORCE;
        chassis_cmd_send.chassis_mode = CHASSIS_ZERO_FORCE;
        chasiss_can_send.robot_status = ROBOT_STOP;
        shoot_cmd_send.shoot_mode = SHOOT_OFF;
        shoot_cmd_send.friction_mode = FRICTION_OFF;
        shoot_cmd_send.load_mode = LOAD_STOP;
        LOGERROR("[CMD] emergency stop!");
    }
    // 遥控器右侧开关为[上],恢复正常运行
    if (switch_is_up(rc_data[TEMP].rc.switch_right))
    {
        robot_state = ROBOT_READY;
        shoot_cmd_send.shoot_mode = SHOOT_ON;
        LOGINFO("[CMD] reinstate, robot ready");
    }
    if (switch_is_mid(rc_data[TEMP].rc.switch_right))
    {
        robot_state = ROBOT_READY;
        shoot_cmd_send.shoot_mode = SHOOT_ON;
        LOGINFO("[CMD] reinstate, robot ready");
    }
    if (switch_is_down(rc_data[TEMP].rc.switch_right))
    {
        robot_state = ROBOT_READY;
        shoot_cmd_send.shoot_mode = SHOOT_ON;
        LOGINFO("[CMD] reinstate, robot ready");
    }
#endif 
#ifdef SENTRY_GIMBAL
    if (robot_state == ROBOT_STOP) // 还需添加重要应用和模块离线的判断
    {
        robot_state = ROBOT_STOP;
        gimbal_cmd_send.gimbal_mode = GIMBAL_ZERO_FORCE;
        chassis_cmd_send.chassis_mode = CHASSIS_ZERO_FORCE;
        shoot_cmd_send.shoot_mode = SHOOT_OFF;
        shoot_cmd_send.friction_mode = FRICTION_OFF;
        shoot_cmd_send.load_mode = LOAD_STOP;
        LOGERROR("[CMD] emergency stop!");
    }
    // 遥控器右侧开关为[上],恢复正常运行
    else
    {
        shoot_cmd_send.shoot_mode = SHOOT_ON;
        LOGINFO("[CMD] reinstate, robot ready");
    }
#endif
}


//初始化完成直接进行任务的写法//

/* 机器人核心控制任务,200Hz频率运行(必须高于视觉发送频率) */
void RobotCMDTask()
{
//注意，接受函数必须放在任务前面，毕竟代码是顺序执行的
//底盘板的任务函数
#ifdef SENTRY_CHASISS
    SubGetMessage(chassis_feed_sub, (void *)&chassis_fetch_data);//底盘与裁判系统的数据传输
    SubGetMessage(gimbal_feed_sub, &gimbal_fetch_data);//大yaw轴的数据传输
    chasiss_can_recv = (CANCommData_s *)CANCommGet(chasiss_can_comm);//云台板给底盘板的数据
    CalcOffsetAngle();//计算归中角度
    // 根据遥控器左侧开关,确定当前使用的控制模式为遥控器调试还是键鼠
    if (switch_is_down(rc_data[TEMP].rc.switch_left)) // 遥控器左侧开关状态为[下],遥控器控制
        RemoteControlSet();
    else if (switch_is_up(rc_data[TEMP].rc.switch_left)) // 遥控器左侧开关状态为[上],键盘控制
    {
         MouseKeySet();
    }
    else if (switch_is_mid(rc_data[TEMP].rc.switch_left))//启动小yaw轴，并测试记载电脑对机器人的控制
    {
        Computer_Control_Set();
    }      
    // chassis_cmd_send.vx = vision_recv_data_zhong->vel_x * -10000;//
    // chassis_cmd_send.vy = vision_recv_data_zhong->vel_y * -10000;//
    // RVSetAttitude(0, 0, 0);
    EmergencyHandler(); // 处理模块离线和遥控器急停等紧急情况  
    // Computer_Control_Set();
    RVSetAttitude(chasiss_can_recv->yaw, chasiss_can_recv->pitch, 0);
    RVRefreeSetAltitude(chassis_fetch_data.current_HP,chassis_fetch_data.stage_remain_time,chassis_fetch_data.game_status,chassis_fetch_data.game_progress);
    // RVRefreeSetAltitude(chassis_fetch_data.current_HP,chassis_fetch_data.stage_remain_time,chassis_fetch_data.game_status,4);
#endif // SENTRY_CHASISS

  

    
//推送消息，双板通信数据发送，视觉通信
#ifdef SENTRY_CHASISS
// VisionSend();//视觉发送（湖大）
RV_VisionSend();//视觉发送（中南）
PubPushMessage(chassis_cmd_pub, (void *)&chassis_cmd_send);//底盘板发送
PubPushMessage(gimbal_cmd_pub, (void *)&gimbal_cmd_send);//云台版发送
CANCommSend(chasiss_can_comm, (void *)&chasiss_can_send);
#endif // SENTRY_CHASISS    
//———————————————————底盘板结束————————————————//
#ifdef SENTRY_GIMBAL
//接收所有数据
gimbal_can_recv = (CANCommData_s *)CANCommGet(gimbal_can_comm);//底盘板给云台板的数据
SubGetMessage(gimbal_feed_sub, &gimbal_fetch_data);
SubGetMessage(shoot_feed_sub, &shoot_fetch_data);
SET_ROBOT();
// robot_state = gimbal_can_recv->robot_status;
//云台板的逻辑 
// CalcOffsetAngle();//计算归中角度
EmergencyHandler();

//发送数据
PubPushMessage(gimbal_cmd_pub, (void *)&gimbal_cmd_send);//云台版发送
PubPushMessage(shoot_cmd_pub, (void *)&shoot_cmd_send);
CANCommSend(gimbal_can_comm, (void *)&gimbal_can_send);
#endif // SENTRY_GIMBAL

    // VisionSetFlag(COLOR_BLUE,VISION_MODE_AIM,SMALL_AMU_15) ;    
    // PubPushMessage(shoot_cmd_pub, (void *)&shoot_cmd_send);
    // 从其他应用获取回传数据
#ifdef ONE_BOARD
    SubGetMessage(chassis_feed_sub, (void *)&chassis_fetch_data);
#endif // ONE_BOARD
#ifdef GIMBAL_BOARD
    chassis_fetch_data = *(Chassis_Upload_Data_s *)CANCommGet(cmd_can_comm);
#endif // GIMBAL_BOARD
    // SubGetMessage(shoot_feed_sub, &shoot_fetch_data);
    // 

    // 根据gimbal的反馈值计算云台和底盘正方向的夹角,不需要传参,通过static私有变量完成
 

    // 设置视觉发送数据,还需增加加速度和角速度数据
    //VisionSetFlag(chassis_fetch_data.enemy_color,VISION_MODE_AIM,chassis_fetch_data.bullet_speed) ;


    // 推送消息,双板通信,视觉通信等
    // 其他应用所需的控制数据在remotecontrolsetmode和mousekeysetmode中完成设置
#ifdef ONE_BOARD
    PubPushMessage(chassis_cmd_pub, (void *)&chassis_cmd_send);
#endif // ONE_BOARD
#ifdef GIMBAL_BOARD
    CANCommSend(cmd_can_comm, (void *)&chassis_cmd_send);
#endif // GIMBAL_BOARD

}